<?php
namespace app\admin\controller;

use app\admin\model\Arctype as ArctypeModel;
use my\Tree;
use Request;

/**
 * 文章分类
 */
class Arctype extends Base
{

    public $data;
    /**
     * 显示文章树形分类列表
     * @return array 文章分类二维数组
     */
    public function index()
    {

        // //$Collection = new Collection($list);
        // $list = Collection::make($list)->toArray();
        // $list = \think\Collection::make($list)->toArray();
        // //echo gettype($list);
        // $tree = new Tree();
        // $data = $tree->toFormatTree($list, 'title');
        //echo \Request::domain();
        $list = ArctypeModel::select();
        $tree = new Tree();
        $data = $tree->tree($list, 'title');
        $this->assign('list', $data);
        return $this->fetch();
    }

    /**
     * 添加分类信息
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Arctype');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = ArctypeModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/Arctype/index');
            }
        }
        $data = ArctypeModel::select();
        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        $this->assign('typename', $data);
        return $this->fetch();
    }

    /**
     * 修改分类信息
     * @return [type] [description]
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Arctype.edit');
            if (true !== $result) {
                return $this->error($result);
            }

            $model = ArctypeModel::edit($id, $post);
            if ($model) {
                addlog($id, $post['title']);
                return $this->success('修改成功', 'admin/Arctypeindex');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = ArctypeModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}
        $data = ArctypeModel::select();

        $tree = new Tree();
        $data = $tree->tree($data, 'title');
        //去掉子栏目（防止，选择子分类,当前栏目）
        $sdata = $tree->channelList($data, $id);
        foreach ($sdata as $key => $v) {
            unset($data[$v['id']]);
        }
        //unset($data[$id]);
        //print_r($data);
        $this->assign([
            'typename' => $data,
            'info'     => $info,
            'typepid'  => $info['pid'],
        ]);
        return $this->fetch();
    }

    /**
     * 删去分类信息
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = ArctypeModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/Arctypeindex', '', 1);
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        //$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            }
            $model = ArctypeModel::update($data);
            //echo $model;
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }
}
